package com.xiaobai.seckill.rabbitmq;

import com.xiaobai.seckill.config.RabbitMQConfig;
import com.xiaobai.seckill.controller.UserController;
import com.xiaobai.seckill.pojo.SeckillMessage;
import com.xiaobai.seckill.pojo.SeckillOrder;
import com.xiaobai.seckill.pojo.User;
import com.xiaobai.seckill.service.IGoodsService;
import com.xiaobai.seckill.service.IOrderService;
import com.xiaobai.seckill.service.impl.GoodsServiceImpl;
import com.xiaobai.seckill.utils.JsonUtil;
import com.xiaobai.seckill.vo.GoodsVo;
import com.xiaobai.seckill.vo.RespBean;
import com.xiaobai.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author shkstart
 * @create 2022-02-07 12:49
 */

/**
 * 消费者
 **/
@Service
@Slf4j
public class MQReceiver {

//    @RabbitListener(queues = "queue")
//    public void receive(Object msg) {
//        log.info("接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_fanout01")
//    public void receive01(Object msg) {
//        log.info("QUEUE01接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_fanout02")
//    public void receive02(Object msg) {
//        log.info("QUEUE02接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_direct01")
//    public void receive03(Object msg) {
//        log.info("QUEUE01接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_direct02")
//    public void receive04(Object msg) {
//        log.info("QUEUE02接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_topic01")
//    public void receive05(Object msg) {
//        log.info("QUEUE01接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_topic02")
//    public void receive06(Object msg) {
//        log.info("QUEUE02接受消息：" + msg);
//    }
//
//    @RabbitListener(queues = "queue_header01")
//    public void receive01(Message message) {
//        log.info("QUEUE01接受Message对象：" + message);
//        log.info("QUEUE01接受消息：" + new String(message.getBody()));
//    }
//
//    @RabbitListener(queues = "queue_header02")
//    public void receive02(Message message) {
//        log.info("QUEUE02接受Message对象：" + message);
//        log.info("QUEUE02接受消息：" + new String(message.getBody()));
//    }

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IOrderService orderService;
    /**
     * 下单操作
     * */
    @RabbitListener(queues = "seckillQueue")
    public void receive(String message) {
        log.info("接收的消息：" + message);
        SeckillMessage seckillMessage = JsonUtil.jsonStr2Object(message,SeckillMessage.class);
        Long goodId = seckillMessage.getGoodId();
        User user = seckillMessage.getUser();

        GoodsVo goodsVo = goodsService.findGoodsVoByGoodsId(goodId);
        if (goodsVo.getStockCount() < 0){
            return;
        }
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:"+user.getId()+":"+goodId);
        if (seckillOrder !=null){
            return;
        }
        orderService.seckill(user,goodsVo);
    }
}
